package com.xtz.controller;

import com.xtz.domain.ShoppingCart;
import com.xtz.dto.ShoppingCartDto;
import com.xtz.dto.SubCartDto;
import com.xtz.service.ShoppingCartService;
import com.xtz.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * date: 2022/5/14 17:06
 *
 * @author xietianze
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加
     * @param dto
     * @return
     */
    @PostMapping("/add")
    private Result add(@RequestBody ShoppingCartDto dto){
        shoppingCartService.add(dto);
        return new Result(1,null,"chenggong",null);
    }

    /**
     * 购物车列表
     * @return
     */
    @GetMapping("/list")
    private Result getList(){
        List<ShoppingCart> list = shoppingCartService.getList();
        return new Result(1,null,list,null);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    private Result clean(){
        Boolean aBoolean = shoppingCartService.cleanCart();
        int code = aBoolean ? 1 : 0;
        String data = aBoolean ? "清空购物车成功" : "清空购物车失败";
        return new Result(code,null,data,null);
    }

    /**
     * 删除购物车商品
     * @return
     */
    @PostMapping("/sub")
    private Result sub(@RequestBody SubCartDto subCartDto){
        Boolean sub = shoppingCartService.sub(subCartDto);
        int code = sub ? 1 : 0;
        return new Result(code,null,null,null);
    }
}
